package com.hyx.bookManager.utils;

import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * @ClassName FileUtil
 * @Description TODO
 * @Author 黄乙轩
 * @Date 2021/5/4 13:29
 * @Version 1.0
 **/

@Component
public class FileUtil {
    public FileUtil(){}

    public String uploadFile(MultipartFile file,String filePath,Long id){
        String fileName = file.getOriginalFilename();
        String suffixName = fileName.substring(fileName.lastIndexOf(".")+1);
        byte[] bytes = null;
        try {
            bytes = file.getBytes();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        Path path = Paths.get(filePath+"/"+id+"."+suffixName);
        try {
            System.out.println(path.toFile().getAbsolutePath());
            Files.write(path,bytes);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        return id+"."+suffixName;
    }
}
